﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAO.Common;
using DTO.Common;
using System.Data;
using System.Data.Common;

namespace DAO.Headquarter
{
    static class Extensions
    {

        #region Product_Extensions

        public static List<ParameterHelper> ToParameterList(this Product product)
        {
            return new List<ParameterHelper> { 
                new ParameterHelper{ DbType = DbType.Int32, Name = "minStock", Value = product.StockMin },
                new ParameterHelper{ DbType = DbType.Int32, Name = "supplier", Value = product.Supplier.Number },
                new ParameterHelper{ DbType = DbType.String, Name = "type", Value = product.Type },
                new ParameterHelper{ DbType = DbType.Int32, Name = "stock", Value = product.Stock }
            };
        }

        public static void FromDataReader(this Product product, DbDataReader reader)
        {
            //    productCode, minStock, type, stock, ordered,
            //supplierNumber, supplierName, supplierAddress

            product.Code        = reader.GetInt32(0);
            product.StockMin    = reader.GetInt32(1);
            product.Type        = (ProductType)Enum.Parse(typeof(ProductType), reader.GetString(2));
            product.Stock       = reader.GetInt32(3);
            product.Ordered     = reader.GetBoolean(4);

            product.Supplier = new Supplier();
            product.Supplier.Number     = reader.GetInt32(5);
            product.Supplier.Name       = reader.GetString(6);
            product.Supplier.Address    = reader.GetString(7);
        }

        #endregion

        #region Supplier_Extensions

        public static List<ParameterHelper> ToParameterList(this Supplier supplier)
        {
            return new List<ParameterHelper> { 
                new ParameterHelper{ DbType = DbType.String, Name = "name", Value = supplier.Name },
                new ParameterHelper{ DbType = DbType.String, Name = "address", Value = supplier.Address }
            };
        }

        public static void FromDataReader(this Supplier supplier, DbDataReader reader)
        {
            supplier.Number     = reader.GetInt32(0);
            supplier.Name       = reader.GetString(1);
            supplier.Address    = reader.GetString(2);
        }

        #endregion

    }
}
